import Vue from "vue"
import Router from "vue-router"
Vue.use(Router)
const router =new Router({
  routes:[
    {path:'/',name:'Index',component:()=>import('@/views/main.vue'),children:[
      {path:'',redirect:'/home'},
      {path:'home',name:'Home',component:()=>import('@/views/home/Home.vue'),meta:{
        title:'工作平台',
        index:'/home'
      }},
      {path:'user',name:'UserList',component:()=>import('@/views/user/UserList.vue'),meta:{
        title:'用户列表',
        index:'/user'
      }},
      {path:'test',name:'Test',component:()=>import('@/views/user/Test.vue'),meta:{
        title:'测试',
        index:'/test'
      }},
      {path:'time',name:'Time',component:()=>import('@/views/user/Time.vue'),meta:{
        title:'时间表',
        index:'/time'
      }},
      {path:'person',name:'Person',component:()=>import('@/views/person/Person.vue'),meta:{
        title:'个人中心',
        index:'/person'
      }},
      {path:'limit',name:'Limit',component:()=>import('@/views/person/Limit.vue'),meta:{
        title:'权限管理',
        index:'/limit'
      }},
    ]},
    {path:'/login',name:'Login',component:()=>import('@/views/account/Login.vue'),meta:{
      title:'登录',
      annoymous:true
    }},
  ]
})

router.beforeEach((to, from, next) => {
  // 更改document.title
  document.title=to.meta.title;
  if(!to.meta.annoymous){
    if(sessionStorage.getItem('username')){
      next();
    }else{
      next('/login')
    }
  }else{
    next();
  }
})

export default router;